C语言如何用cJSON解析JSON格式 您所在的位置:网站首页 编程常用术语 解析 C语言如何用cJSON解析JSON格式

C语言如何用cJSON解析JSON格式

2023-04-29 17:50| 来源: 网络整理| 查看: 265

C语言怎么用cJSON解析JSON格式

本文讲解"C语言如何用cJSON解析JSON格式",希望能够解决相关问题。

一、JSON格式

语法:键 / 值

1、以 { 开始,以 } 结束,允许嵌套使用

2、每个键和值成对出现,并使用:分隔。如"age"=23

3、键值对之间用 ,分隔

值的多种类型:

字符串:用 " "

{     "name":"code",     "gender":"male" }

数字:整数或浮点数都直接表示

{     "key1":10,     "key2":20.0 }

数组:用[ ]

 {      "key1" : [0, 1],      "key2" : [2, 3]  }

布尔值:fault、true

二、cJSON下载git clone https://gitee.com/peng-jiaweibabe/c-json.git

cJSON的.c和.h文件,使用的时候,只需要将这两个文件复制到工程目录,然后包含头文件cJSON.h即可。即#include "cJSON.h"

如若出现该情况,链接math库即可 

C语言怎么用cJSON解析JSON格式

三、cJSON常用函数接口1.cJSON_Parse

CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) 函数功能:将一个JSON字符串,按照cJSON结构体的结构序列化整个数据包,并在堆中开辟一块内存存储cJSON结构体 返回值:成功返回一个指向内存块中的cJSON的指针,失败返回NULL

2.cJSON_Print

CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)    //按JSON格式打印    CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)    //不按JSON格式打印 函数功能:将整条链表中存放的JSON信息输出到一个字符串中,使用时只需用一个字符串指针(char *)接收该函数返回的指针地址即可。 返回值:成功返回一个char*指针并指向位于堆中JSON字符串,失败返回NULL

3.cJSON_Delete

CJSON_PUBLIC(void) cJSON_Delete(cJSON *c) 函数功能:释放位于堆中cJSON结构体内存 返回值:无

4.cJSON_GetObjectItem

(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string) 函数功能:根据键值对的名称从链表中取出对应的值,返回该键值对(链表节点)的地址 返回值:成功返回一个指向内存块中的cJSON的指针,失败返回NULL

5.cJSON_GetObjectItem(数组相关)CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string) (int) cJSON_GetArraySize(const cJSON *array) (cJSON *) cJSON_GetArrayItem(const cJSON *array, int index)6.创建对象函数接口/* raw json */ CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw); CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); /* These calls create a cJSON item of the appropriate type. */ CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void); CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void); CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void); CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean); CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num); CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string); /* These utilities create an Array of count items. */ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count); CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count); CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count); CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count);7.添加cJSON对象到链表CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item); CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);8.从现有的cJSON链表中删除一个对象CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item); CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which); CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string); CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);四、cJSON解析JSON案例1.一层键值/*********************************************************************************  *      Copyright:  (C) 2022 Nbiot  *                  All rights reserved.  *  *       Filename:  test_cjson.c  *    Description:  This file test_cjson.c  *  *        Version:  1.0.0(30/05/22)  *         Author:  Nbiot   *      ChangeLog:  1, Release initial version on "30/05/22 20:25:49"  *  ********************************************************************************/ #include  #include  #include "cJSON.h"   int main (int argc, char **argv) {         char json_buf[] = "{\"type\":\"text\",\"number\":1111,\"sms\":\"nihao\"}";           cJSON *json = NULL;         cJSON *json_type = NULL;         cJSON *json_num = NULL;         cJSON *json_sms = NULL;           printf("json格式化前:\n");         printf("%s\n\n", json_buf);           json = cJSON_Parse(json_buf);   //json格式序列化           if (NULL == json)         {                 printf("cJSON_Parse error:%s\n", cJSON_GetErrorPtr());         }           printf("json格式化后:\n");         printf("%s\n\n",cJSON_Print(json));           /* 获取相应key的value */         json_type = cJSON_GetObjectItem(json, "type");         json_num = cJSON_GetObjectItem(json, "number");         json_sms = cJSON_GetObjectItem(json, "sms");           printf("type:%s\n", json_type->valuestring);         printf("number:%d\n", json_num->valueint);         printf("sms:%s\n", json_sms->valuestring);           cJSON_Delete(json);             //释放cjson结构体内存           return 0; }

结果:

C语言怎么用cJSON解析JSON格式

2.多层键值(两次为例)/*********************************************************************************  *      Copyright:  (C) 2022 Nbiot  *                  All rights reserved.  *  *       Filename:  test_cjson1.c  *    Description:  This file test_cjson1.c  *  *        Version:  1.0.0(30/05/22)  *         Author:  Nbiot   *      ChangeLog:  1, Release initial version on "30/05/22 23:36:09"  *  ********************************************************************************/ #include  #include  #include "cJSON.h"   int main (int argc, char **argv) {         char json_buf[] =  "{\"type\":\"text\",\"number\":{\"phone_number\":\"17687499242\"},\"sms\":\"nihao\"}";           cJSON *json = NULL;         cJSON *json_phone_number = NULL;           printf("json格式化前:\n");         printf("%s\n\n", json_buf);           json = cJSON_Parse(json_buf);   //json格式序列化           if (NULL == json)         {                 printf("cJSON_Parse error:%s\n", cJSON_GetErrorPtr());         }           printf("json格式化后:\n");         printf("%s\n\n",cJSON_Print(json));           /* 获取相应key的value */         json_phone_number = cJSON_GetObjectItem(json, "number");    //首先获取第一次键值           json_phone_number = cJSON_GetObjectItem(json_phone_number, "phone_number");    //获取第二层           printf("phone_number:%s\n", json_phone_number->valuestring);           cJSON_Delete(json);             //释放cjson结构体内存           return 0; }

结果:

C语言怎么用cJSON解析JSON格式

3.json数组解析/*********************************************************************************  *      Copyright:  (C) 2022 Nbiot  *                  All rights reserved.  *  *       Filename:  test_cjson3.c  *    Description:  This file test_cjson3.c  *  *        Version:  1.0.0(31/05/22)  *         Author:  Nbiot   *      ChangeLog:  1, Release initial version on "31/05/22 00:14:13"  *  ********************************************************************************/ #include  #include  #include "cJSON.h"   int main (int argc, char **argv) {         char json_buf[] = "{\"type\":\"text\",\"number\":1111,\"sms\":\"nihao\",\"array\":[1,2,3]}";           cJSON *json = NULL;         cJSON *json_array = NULL;         int array_size=0;         cJSON *json_array_value = NULL;           printf("json格式化前:\n");         printf("%s\n\n", json_buf);           json = cJSON_Parse(json_buf);   //json格式序列化           if (NULL == json)         {                 printf("cJSON_Parse error:%s\n", cJSON_GetErrorPtr());         }           printf("json格式化后:\n");         printf("%s\n\n",cJSON_Print(json));           json_array = cJSON_GetObjectItem(json, "array");             array_size = cJSON_GetArraySize(json_array);    //获取数组大小           printf("array_size=%d\n",array_size);           for(int i=0; ivalueint);         }           cJSON_Delete(json);             //释放cjson结构体内存           return 0; }

 结果:

C语言怎么用cJSON解析JSON格式

五、JSON添加数据 (与链表类似)

三层键值

/*********************************************************************************  *      Copyright:  (C) 2022 Nbiot  *                  All rights reserved.  *  *       Filename:  test_cjson2.c  *    Description:  This file test_cjson2.c  *  *        Version:  1.0.0(30/05/22)  *         Author:  Nbiot   *      ChangeLog:  1, Release initial version on "30/05/22 20:46:57"  *  ********************************************************************************/ #include  #include  #include "cJSON.h"   int main(int argc, char **argv) {         char *json_ptr = NULL;         cJSON * root   =  cJSON_CreateObject();         cJSON * son    =  cJSON_CreateObject();         cJSON * next   =  cJSON_CreateObject();           cJSON_AddItemToObject(root, "gender", cJSON_CreateString("male"));         cJSON_AddItemToObject(root, "student", son); //第一层嵌套键值         cJSON_AddItemToObject(son,      "name", cJSON_CreateString("xiaochen"));//第二层嵌套键值         cJSON_AddItemToObject(son,      "school", next); //第二层嵌套键值         cJSON_AddItemToObject(next, "name", cJSON_CreateString("high school"));//第三层嵌套键值           json_ptr = cJSON_Print(root);         printf("JSON:\n", json_ptr);         printf("%s\n", json_ptr);           free(json_ptr);           cJSON_Delete(root);           return 0; }

结果:

C语言怎么用cJSON解析JSON格式

关于 "C语言如何用cJSON解析JSON格式" 就介绍到此。希望多多支持编程宝库。

下一节:C++ 获取当前正在运行函数的名称C语言编程技术

 func、FUNCTION、__PRETTY_FUNCTION__的区别常用获取函数名成的方法都有__func__、FUNCTION、PRETTY_FUNCTION。那么它们的区别是什么呢?1 ...



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有